<?php
/*
 * 环境获取类
 * @author     Mark <zhaody901@126.com>
 * @copyright  Copyright (c) 2013 lemon team
 * @version    $Id: 9dfc84bf07c44787456ee58a312c5ccab4b50475 $
 * @time       14-2-7 上午10:27
 */
class Env
{

	/**
	 * @return string 返回IP
	 */
	public static function ip()
	{
		isset($_SERVER['HTTP_X_FORWARDED_FOR']) or $_SERVER['HTTP_X_FORWARDED_FOR'] = '';
		isset($_SERVER['REMOTE_ADDR']) or $_SERVER['REMOTE_ADDR'] = '';
		isset($_SERVER['HTTP_CLIENT_IP']) or $_SERVER['HTTP_CLIENT_IP'] = '';
		if($_SERVER['HTTP_X_FORWARDED_FOR'] && $_SERVER['REMOTE_ADDR']) {
			$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
			if(strpos($ip, ',') !== false) {
				$tmp = explode(',', $ip);
				$ip = trim(end($tmp));
			}
			if(Utils::isIp($ip)) return $ip;
		}
		if(Utils::isIp($_SERVER['HTTP_CLIENT_IP'])) return $_SERVER['HTTP_CLIENT_IP'];
		if(Utils::isIp($_SERVER['REMOTE_ADDR'])) return $_SERVER['REMOTE_ADDR'];
		return 'unknown';
	}

	/**
	 * @return string 当前文件的名称
	 */
	public static function self()
	{
		return isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : (isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : $_SERVER['ORIG_PATH_INFO']);
	}

	/**
	 * @return string 来源地址
	 */
	public static function referer()
	{
		return isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
	}

	/**
	 * @return string 返回服务器名称
	 */
	public static function domain()
	{
		return $_SERVER['SERVER_NAME'];
	}

	/**
	 * @return string 协议名称
	 */
	public static function scheme()
	{
		return $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
	}

	/**
	 * @return string 返回端口号
	 */
	public static function port()
	{
		return $_SERVER['SERVER_PORT'] == '80' ? '' : ':'.$_SERVER['SERVER_PORT'];
	}

	/**
	 * @return string 完整的地址
	 */
	public static function uri()
	{
		if(isset($_SERVER['REQUEST_URI'])) {
			$uri = $_SERVER['REQUEST_URI'];
		} else {
			$uri = $_SERVER['PHP_SELF'];
			if(isset($_SERVER['argv'])) {
				if(isset($_SERVER['argv'][0])) $uri .= '?'.$_SERVER['argv'][0];
			} else {
				$uri .= '?'.$_SERVER['QUERY_STRING'];
			}
		}
		$uri = Str::htmlSpecialChars($uri);
		return self::scheme().$_SERVER['HTTP_HOST'].(strpos($_SERVER['HTTP_HOST'], ':') === false ? self::port() : '').$uri;
	}

	/**
	 * @return string   没有查询的完整的URL地址, 基于当前页面
	 */
	public static function nquri()
	{
		return self::scheme().$_SERVER['HTTP_HOST'].(strpos($_SERVER['HTTP_HOST'], ':') === false ? self::port() : '').self::self();
	}

	public static function time() {
		return $_SERVER['REQUEST_TIME'];
	}
	/**
	 * 检测来源
	 * @return bool
	 */
	public static function checkReferer() {
		global $DT_REF, $CFG, $DT;
		// 验证数据来源
		if ($DT['check_referer']) {
			// 没有来源
			if (!$DT_REF) return false;
			$R = parse_url($DT_REF);
			// cookie domain验证
			if ($CFG['cookie_domain'] && strpos($R['host'], $CFG['cookie_domain']) !== false) return true;
			// 公司域名验证
			if ($CFG['com_domain'] && strpos($R['host'], $CFG['com_domain']) !== false) return true;
			// 信任域名
			if ($DT['safe_domain']) {
				$tmp = explode('|', $DT['safe_domain']);
				foreach ($tmp as $v) {
					if (strpos($R['host'], $v) !== false) return true;
				}
			}
			$U = parse_url(SITE_URL);
			if (strpos($R['host'], str_replace('www.', '.', $U['host'])) !== false) return true;
			return false;
		} else {
			return true;
		}
	}


	/**
	 * 检测提交方式是否是 POST 方法
	 * @return bool
	 */
	public static function checkPost() {
		if (strtoupper($_SERVER['REQUEST_METHOD']) != 'POST') return false;
		return self::checkReferer();
	}
}